//
//  YMMenuCollectionView.swift
//  NewVersionDemo
//
//  Created by zeng on 2018/3/15.
//  Copyright © 2018年 youmy. All rights reserved.
//

import UIKit

fileprivate let kCellId = "kCellId"

class YMMenuCollectionView: UIView {
    
    fileprivate var itemTitle:String?
    
    fileprivate lazy var collectionView:UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        let width = (UIScreen.main.bounds.width - 40 - 26) / 3
        layout.itemSize = CGSize(width: width, height: 30)
        layout.minimumLineSpacing = 13
        layout.minimumInteritemSpacing = 13
        layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20)
        
        let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.collectionViewLayout = layout
        collectionView.register(UINib.init(nibName: "YMMenuCell", bundle: nil), forCellWithReuseIdentifier: kCellId)
        collectionView.dataSource = self
        collectionView.delegate = self
        
        return collectionView
    }()
    
    var didSelBlcok:((_ title: String)->())?
    
    var menus:[String]?{
        didSet{
            let count = (menus?.count ?? 0) / 3
            
            if (menus?.count ?? 0) % 3 != 0{
                self.frame.size.height = CGFloat(30 * (count + 1) + 40) + CGFloat(13 * count)
            }else{
                self.frame.size.height = CGFloat(30 * count + 40) + CGFloat(13 * (count - 1))
            }
            collectionView.frame = bounds
            collectionView.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        autoresizingMask = UIViewAutoresizing()
        addSubview(collectionView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension YMMenuCollectionView: UICollectionViewDataSource, UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return menus?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kCellId, for: indexPath) as! YMMenuCell
        cell.titleLbl.text = menus?[indexPath.item]
        if itemTitle == cell.titleLbl.text {
            cell.titleLbl.textColor = ym_red_color
        }else{
            cell.titleLbl.textColor = ym_light_back_color
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let cells = collectionView.visibleCells
        let cell = collectionView.cellForItem(at: indexPath) as! YMMenuCell

        for obj in cells {
            if obj is YMMenuCell {
                let cell = obj as! YMMenuCell
                cell.titleLbl.textColor = ym_light_back_color
            }
        }
        cell.titleLbl.textColor = ym_red_color
        itemTitle = menus?[indexPath.item]
        print("点击：\(indexPath.item)")
        if didSelBlcok != nil {
            let title = "\(itemTitle ?? "") "
            didSelBlcok?(title)
        }
    }
}
